/********************************************************************************
 * Project Name		[JavaSE_Fundamentals]
 * File Name     	[TestStringTokenizer.java]
 * Creation Date 	[2015-01-01]
 * 
 * Copyright© ge.y.yang@gmail.com All Rights Reserved
 * 
 * Work hard, play harder, think big and keep fit
 ********************************************************************************/
package api.util;

import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

/**
 * java.util.StringTokenizer测试
 * 
 * @author 不落的太阳(Sean Yang aka ShortPeace)
 * @version 1.0
 * @since jdk 1.8
 * 
 */
public class TestStringTokenizer {

	/** 默认分隔符 */
	public final static String DELIM = ",";

	public static String[] process(String line) {
		return process(line, DELIM, false);
	}

	public static String[] process(String line, String delim) {
		return process(line, delim, false);
	}

	/**
	 * 用StringTokenizer分割字符串
	 * 
	 * @param line
	 *            待分割的字符串
	 * @param delim
	 *            分割符
	 * @param returnDelims
	 *            是否返回分隔符, 默认为false
	 * @return 被分割后的字符串数组
	 */
	public static String[] process(String line, String delim, boolean returnDelims) {

		List<String> results = new ArrayList<String>();
		// 新建一个StringTokenizer对象
		StringTokenizer st = new StringTokenizer(line, delim, returnDelims);
		// 循环, 如果字符串中还有分隔符, 则继续
		while (st.hasMoreTokens()) {
			// 取上一个分隔符到下一个分隔符之间的字符串
			String s = st.nextToken();
			// 将中间的字符串添加到结果列表中
			results.add(s);
		}
		return results.toArray(new String[0]);
	}

	/**
	 * 输出分割结果
	 * 
	 * @param input
	 * @param outputs
	 */
	public static void printResults(String input, String[] outputs) {
		System.out.println("Input: " + input);
		for (int i = 0; i < outputs.length; i++) {
			System.out.println("Output " + i + " was: " + outputs[i]);
		}
	}

	public static void main(String[] args) {
		printResults("A|B|C|D", process("A|B|C|D", "|"));
		printResults("A||C|D", process("A||C|D", "|", true));
		printResults("A|||D|E", process("A|||D|E", "|", false));
		printResults("A;BCD;|E;FG", process("A;BCD;|E;FG", ";"));
		printResults("A;BCD;|E;FG;HIJK;LMN", process("A;BCD;|E;FG;HIJK;LMN", ";", false));
	}
}
